package com.lmb.todo.controller;

import com.lmb.todo.pojo.Group;
import com.lmb.todo.pojo.resp.Code;
import com.lmb.todo.pojo.resp.CommonResp;
import com.lmb.todo.service.impl.GroupServiceImpl;
import com.lmb.todo.utils.AuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;

/**
 * @author lmc
 */
@RestController
@RequestMapping("/api/group")
public class GroupController {

    @Autowired
    private GroupServiceImpl groupService;

    @Autowired
    private AuthUtil authUtil;

    @GetMapping("")
    public CommonResp get() {
        HashMap userInfo = authUtil.getAuthUserInfo();
        Integer userId = (Integer) userInfo.get("userId");

        List<Group> groupList = groupService.getAllByUserId(userId);

        if (groupList != null) {
            return new CommonResp(
                    groupList,
                    Code.HTTP_SUCCESS,
                    "ok!"
            );
        }

        return new CommonResp(
            null,
            Code.S_UNKNOWN_ERROR,
            "error!"
        );
    }

    /**
    * 更改分组数据信息
    * */
    @PutMapping("")
    public CommonResp update(@RequestBody Group group){
        Integer count = groupService.update(group);
        CommonResp resp = new CommonResp();
        resp.setData(null);

        if (count > 0) {
            resp.setCode(Code.HTTP_SUCCESS);
            resp.setMsg("OK!");
            return resp;
        }
        resp.setCode(Code.C_UPDATE_ERROR);
        resp.setMsg("update failing.");
        return resp;
    }

    /**
    * 根据分组id删除分组信息
    * */
    @DeleteMapping("/{id}")
    public CommonResp delete(@PathVariable Integer id){
        Integer count = groupService.delete(id);
        CommonResp resp = new CommonResp();
        resp.setData(null);

        if (count > 0) {
            resp.setCode(Code.HTTP_SUCCESS);
            resp.setMsg("OK!");
            return resp;
        }
        resp.setCode(Code.C_DELETE_ERROR);
        resp.setMsg("delete failing.");
        return resp;
    }

    /**
     * 根据分组folderId 移动分组到文件夹下
     * */
    @PutMapping("/move")
    public CommonResp move(Integer folderId, Integer id) {
        CommonResp resp = new CommonResp();
        Integer count;

        try {
            count = groupService.move(folderId == -1 ? null : folderId, id);
        } catch (Exception e) {
            resp.setCode(Code.C_FOREIGN_KEY_ERROR);
            resp.setMsg("param error.");
            return resp;
        }

        if (count > 0) {
            resp.setCode(Code.HTTP_SUCCESS);
            resp.setMsg("OK!");
            return resp;
        }
        resp.setCode(Code.S_UNKNOWN_ERROR);
        resp.setMsg("move failing.");
        return resp;
    }

    /**
    * 新增分组
    * */
    @PostMapping("")
    public CommonResp add(@RequestBody Group group){
        HashMap userInfo = authUtil.getAuthUserInfo();
        Integer userId = (Integer) userInfo.get("userId");
        group.setUserId(userId);

        CommonResp resp = new CommonResp();
        Integer count = groupService.add(group);

        if (count > 0) {
            resp.setCode(Code.HTTP_SUCCESS);
            resp.setMsg("OK!");
            return resp;
        }
        resp.setCode(Code.C_INSERT_ERROR);
        resp.setMsg("insert failing.");
        return resp;
    }
}
